package cn.com.wxd.service.sms.chuanglan;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

import cn.com.wxd.service.sms.SmsInfo;
import cn.com.wxd.service.sms.SmsSenter;
import cn.com.wxd.common.CharUtil;
import cn.com.wxd.common.CharValidator;
import cn.com.wxd.common.JsonUtil;
import cn.com.wxd.common.thread.ThreadPoolUtil;

/**
 * Title:创蓝短信服务
 * Description:cn.com.wxd.util.service.sms
 * 
 * Company: Amumu管理平台
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created time: 2016-4-8 下午12:24:15
 *
 * @author WangXuDong
 * @version 1.0
 */
public class SmsService_chuanglan implements SmsSenter {
	ChuanglanSMS client = new ChuanglanSMS(SmsInfo.getUserName(),SmsInfo.getPassword());
	CloseableHttpResponse response = null;
	private static final Logger log = LoggerFactory.getLogger(SmsService_chuanglan.class);
	@Override
	public int sent(String phoneNum, String content) {
		if(!CharValidator.isMobile(phoneNum)){
			log.info("发送短信---手机号码错误!");
			return -1;
		}
		if(content==null||content.trim().equals("")){
			log.info("发送短信---内容不能为空!");
			return -2;
		}
		//发送短信
		try {
			response = client.sendMessage(phoneNum,content);
			if(response != null && response.getStatusLine().getStatusCode()==200){
				return 1;
			}else{
				log.error("短信发送失败");
				return 0;
			}
		} catch (Exception e) {
			log.error("短信发送失败",e);
			return 0;
		}
	}
	@Override
	public int sent(String[] phoneNums, String content) {
		if(content==null||content.trim().equals("")){
			log.error("短信发送内容空");
			return -2;
		}
		if(phoneNums==null||phoneNums.length<=0){
			for (String phoneNum : phoneNums) {
				if (!CharValidator.isMobile(phoneNum)) {
					log.info("发送短信---手机号码错误!");
					return -1;
				}
			}
			log.info("短信发送服务---请设置手机号！");
			return -1;
		}
		if(queryBalance()<100){
			log.info("短信发送服务---余额不足！");
			return -3;
		}
		String phoneNum;
		for (int i = 0; i < phoneNums.length; i++) {
			phoneNum = phoneNums[i];
			sent(phoneNum,content);
		}
		return 1;
	}
	@Override
	public int queryBalance() {
		try {
			response = client.queryBalance();
			if(response != null && response.getStatusLine().getStatusCode()==200){
				String json = EntityUtils.toString(response.getEntity());
				Map<String,String> map = JsonUtil.jsonToStrMap(json);
				String num = map.get("bl");
				return CharUtil.stringParsInt(num);
			}else{
				return -1;
			}
		} catch (Exception e) {
			log.error("余额查询失败",e);
			return -1;
		}
	}

	@Override
	public int asyncSent(final String phoneNum, final String content) {
		if (content == null || content.trim().equals("")) {
			log.error("短信发送内容空");
			return -2;
		}
		if (phoneNum == null || !CharValidator.isMobile(phoneNum)) {
			return -1;
		}
		try {
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					sent(phoneNum, content);
				}
			};
			ThreadPoolUtil.submintThread(runnable);
			return 1;
		}catch (Exception e){
			log.error("短信发送失败!",e);
			return 0;
		}
	}

	@Override
	public int asyncSent(final String[] phoneNums, final String content) {
		if (content == null || content.trim().equals("")) {
			log.error("短信发送内容空");
			return -2;
		}
		if (phoneNums == null || phoneNums.length <= 0) {
			if (log.isInfoEnabled()) {
				log.info("短信发送服务---请设置手机号！");
			}
			return -1;
		}
		try {
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					sent(phoneNums, content);
				}
			};
			ThreadPoolUtil.submintThread(runnable);
			return 1;
		}catch (Exception e){
			log.error("短信发送失败!",e);
			return 0;
		}
	}

}
